<script setup lang="ts">
import './node-style.scss'
import {inject, ref} from "vue";
import {Node} from "@antv/x6";
import {WebDiskDatabaseApi} from "@/api/ai/webdiskdatabase";

const dbs=ref<any[]>()
const formData = reactive<any>({
  dbId:undefined,
  field:undefined
})
const init = async () => {
  dbs.value=await WebDiskDatabaseApi.simpleList({})
}
const id=ref<string>()
const getNode=inject<()=>Node>('getNode')
if(getNode){
  const node=getNode()
  id.value=node.id
  node.setData(formData)
}
onBeforeMount(()=>{
  if(getNode){
    const node=getNode()
    const d=node.getData()
    if (d){
      Object.assign(formData,d)
    }else {
      node.setData(formData)
    }
  }
})
onMounted(() => {
  init()
})
watch(formData,()=>{
  if(getNode){
    const node=getNode()
    node.setData(formData)
  }
},{deep:true,immediate:false})
</script>

<template>
  <div class="node">
    <div class="node-title line5">
      <div class="flow-dot" magnet="true" :class="'flow-exec'" x-type="input-exec" :x-id="'input-exec_'+id"></div>
      <div>保存到Qdrant数据库</div>
      <div class="flow-dot flow-right flow-exec" magnet="true" x-type="output-exec" :x-id="'output-exec_'+id"></div>
    </div>
    <el-form
      :model="formData"
      label-position="top"
      @mousedown.stop
      @focus.stop
      style="cursor: default"
    >
      <el-form-item label="数据库">
        <el-select v-model="formData.dbId" clearable placeholder="请选择数据库">
          <el-option v-for="db in dbs" :key="db.id" :label="db.name" :value="db.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="检索字段">
        <el-input v-model="formData.field" placeholder="用来文本搜索"/>
      </el-form-item>
    </el-form>
    <div>
      <div class="line5">
        <div class="flow-dot flow-array" magnet="true" x-type="input-array-json" :x-id="'input-array-json_'+id"></div>
        <div>文件分片</div>
      </div>
      <div style="color: #9f9f9f;font-size: 15px">元数据</div>
      <div class="line5">
        <div class="flow-dot flow-string" magnet="true" x-type="input-url" :x-id="'input-url_'+id"></div>
        <div>URL</div>
      </div>
      <div class="line5">
        <div class="flow-dot flow-string" magnet="true" x-type="input-id" :x-id="'input-id_'+id"></div>
        <div>文件ID</div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">

</style>
